package com.view;

import android.content.Context;
import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;

import hcvs.audio.AACEncoder;
import hcvs.hcvsa.HCVSA;
import hcvs.hcvsa.TPVideoData;
import hcvs.videorender.MySurfaceView;

public class MyTPSurfaceView extends MySurfaceView {
    private static final String TAG = "MyTPSurfaceView";
    /*C++投屏类指针*/
    private int pVideoData;
    private int type;
    private int mId;
    private int channel;
    private TPVideoData videoData;
    private Context mContext;

    private AACEncoder mAACEncoder;


    @RequiresApi(api = Build.VERSION_CODES.N)
    public MyTPSurfaceView(Context context, int pVideoData) {
        super(context);
        mContext    = context;
        this.pVideoData = pVideoData;
        init();

    }

    @Override
    public void release() {
        Log.d(TAG, "MyTPSurfaceView release");
        if (mAACEncoder != null){
            mAACEncoder.stop();
            mAACEncoder = null;
        }
        super.release();
    }

    @Override
    protected void finalize() throws Throwable {
        Log.d("MyTPSurfaceView", "finalize。。。");
        super.finalize();
    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void init(){
//        performCaptureAndAACEncode();
        videoData = new TPVideoData();
        boolean isOK = videoData.Init(pVideoData);
        videoData.setOnTPVideoData(new TPVideoData.OnTPVideoData() {
            @Override
            public void DeliverVideoData(byte[] data, int nLen, short cx, short cy, boolean bKeyFrame, int timestamp) {

//                long time1  = System.currentTimeMillis();
                decode(data, 0, nLen, null, 0);
//                long time2  = System.currentTimeMillis();
////                Log.i("df", "处理一帧用时："+(time2-time1)+", 与上一帧时间差="+(time2-lastTime));
////                lastTime    = time2;
            }

            @Override
            public void OnDeliverAudioData(byte[] data, int nLen, int timestamp, int rate) {
//                Log.d(TAG, "OnDeliverAudioData");
                if (HCVSA.GetAudioPlayMute() != HCVSA.HCVS_VIDEO_MUTE){
                    playAudio(data, nLen, rate);
                }
//                mAACEncoder.putAudioData(data);

            }

            @Override
            public void OnVTDUConnectStatus(int i) {

            }
        });
    }

    public int getpVideoData() {
        return pVideoData;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getmId() {
        return mId;
    }

    public void setmId(int mId) {
        this.mId = mId;
    }

    public int getChannel() {
        return channel;
    }

    public void setChannel(int channel) {
        this.channel = channel;
    }
}
